﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Core
{
    public class HCHelpMapContainer
    {
        private List<HelpMap> _helpMaps = new List<HelpMap>();

        [XmlArray("helpmaps"), XmlArrayItem("helpmap")]
        public List<HelpMap> HelpMap
        {
            get { return _helpMaps; }
            set { _helpMaps = value; }
        }

        private string _filePath = string.Empty;

        [XmlIgnore]
        public string FilePath
        {
            get { return _filePath; }
            set { _filePath = value; }
        }

        public void SaveToDisk()
        {
            if (HCProjectManager.CurrentProject != null)
            {
                /*
                 * make up file path
                 * save file
                 */
                string fileFullName = string.Concat(HCProjectManager.CurrentProject.ProjectFolder.FullName, "\\", HCProjectManager.CurrentProject.HCId, ".xml");

                if (HCProjectManager.CurrentProject.HelpMapContainer != null)
                {
                    string helpMapXml = Utils.Serialize(HCProjectManager.CurrentProject.HelpMapContainer, typeof(HCHelpMapContainer));
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.LoadXml(helpMapXml);
                    string innerXml = xmlDoc.ChildNodes[1].InnerXml;
                    innerXml = string.Concat("<?xml version=\"1.0\" encoding=\"utf-8\"?>", innerXml);

                    using (StreamWriter swrite = new StreamWriter(fileFullName))
                    {
                        swrite.Write(innerXml);
                    }

                    this.FilePath = fileFullName;
                }
            }
        }
    }
}
